package com.gtfrank.web.servlet.util;

import java.sql.*;

/**
 * @ProjectName: jdbcourse
 * @ClassName: JdbcUtil
 * @PackageName:com.jdbc.util
 * @Description: 改版的工具类
 * @Author: frank
 * @Date: 2021/3/19 15:14
 * @Version v1.0
 */
public class JdbcUtil {
    public static String DB_DRIVER = null;
    public static String DB_URL = null;
    public static String DB_USER = null;
    public static String DB_PASSWORD = null;
    static{
        DB_DRIVER = DBConfig.getInstance().getValue("driverClassName");
        DB_URL = DBConfig.getInstance().getValue("dburl");
        DB_USER = DBConfig.getInstance().getValue("dbuser");
        DB_PASSWORD = DBConfig.getInstance().getValue("dbpassword");
    }

    public  static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName(DB_DRIVER);
        return DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
    }
    public  static void relase(ResultSet resultSet, Statement statement,Connection connection){
        if(resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

    public  static void relase(Object object){
        if(object instanceof  ResultSet){
            ResultSet resultSet = (ResultSet) object;
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(object instanceof  Statement){
            Statement statement = (Statement) object;
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(object instanceof  Connection){
            Connection connection = (Connection) object;
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }


    }
    //支持  insert  update  delete 这三种sql语句 及其不同sql参数个数不一样

    /**
     * 1:因为我不知道你执行什么sql语句，所有你传入sql
     * 2:你的参数个数不确定   什么容器可以 存放 >=1 集合 数组  可变长参数【String... args】
     * 此代码还不是最终版本，等讲到事务的时候 会把代码更新
     */
    public static void updateData(String sql,Object... args){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try{
            connection  = JdbcUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            //迭代可变长参数
            for(int i =0;i<args.length;i++){
                preparedStatement.setObject(i+1,args[i]);
            }
            int i = preparedStatement.executeUpdate();
            System.out.println(i+"条记录被影响");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JdbcUtil.relase(null,preparedStatement,connection);
        }

    }

    /**
     * 列名=值
     * @param resultSet
     */
    public static void  printResult(ResultSet resultSet){
       StringBuffer jpsb = new StringBuffer();
        try{
            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
            int count = resultSetMetaData.getColumnCount();
            while(resultSet.next()){
                jpsb.append("[");
                for (int i =1;i<=count;i++){
                    jpsb.append(resultSetMetaData.getColumnName(i)+" =" +
                            ""+resultSet.getObject(resultSetMetaData.getColumnName(i))+"  ");
                }
                jpsb.append("]").append("\n");
            }
            System.out.println(jpsb.toString());
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    public static void main(String[] args) {
        try {
            System.out.println(JdbcUtil.getConnection());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
